package com.dq.domain.category;

import com.alibaba.fastjson.JSONObject;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;

import com.dq.domain.sys.section.SysSection;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/9/11 0011 15:57:17
 */
@Data
@Entity
@ApiModel("商品分类")
public class Category extends BaseEntity implements VO.ToVO {

    @ManyToOne
    @ApiModelProperty(value = "类别父对象", hidden = true)
    protected Category parentCategory;

    @ApiModelProperty("分类标题")
    protected String title;

    @ApiModelProperty("排序")
    protected Long sortNo;

    @ApiModelProperty("iconUrl")
    protected String iconUrl;

    @ApiModelProperty("descUrl")
    protected String descUrl;

    @ApiModelProperty("层级")
    protected Long level;

    @ApiModelProperty("描述")
    protected String simpleDesc;

    @ManyToOne
    @ApiModelProperty("所属门店")
    protected SysSection org;

    @Data
    @ApiModel(value = "分类视图")
    public static class Vo implements VO {
        @ApiModelProperty("id")
        private Long id;
        @ApiModelProperty("分类标题")
        private String title;
        @ApiModelProperty("排序")
        @NotNull
        private Long sortNo;
        @ApiModelProperty("iconUrl")
        private String iconUrl;
        @ApiModelProperty("descUrl")
        protected String descUrl;
        @ApiModelProperty("层级")
        private Long level;
        @ApiModelProperty("描述")
        private String simpleDesc;
        @ApiModelProperty("创建人")
        private String createUserName;
        @ApiModelProperty("最后修改时间")
        private String lastModifyTime;
        @ApiModelProperty("创建时间")
        private String createTime;
        @ApiModelProperty("父级id")
        private Long parentId;
        @ApiModelProperty("父级名称")
        private String parentTitle;
        @ApiModelProperty("子类别")
        private List<Vo> childs = new ArrayList<>();
        @ApiModelProperty("机构id")
        private Long orgId;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.parentId = this.parentCategory == null ? null : this.parentCategory.id;
        vo.parentTitle = this.parentCategory == null ? "" : this.parentCategory.title;
        vo.title = this.title;
        vo.sortNo = this.sortNo;
        vo.iconUrl = this.iconUrl;
        vo.descUrl = fmt(this.descUrl);
        vo.level = this.level;
        vo.simpleDesc = this.simpleDesc;
        vo.createUserName = this.createUser == null ? "" : fmt(this.createUser.getUsername());
        vo.lastModifyTime = fmt(this.getLastModifyTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.orgId = this.org == null ? null : this.org.getId();
        return vo;
    }
    public JSONObject toSimpleVO() {
        JSONObject vo = new JSONObject();

        vo.put("id", this.id);
        vo.put("sortNo", this.sortNo);
        vo.put("title", this.title);
        vo.put("parentId",this.parentCategory == null ? null : this.parentCategory.id);
        vo.put("parentTitle", this.parentCategory == null ? "" : this.parentCategory.title);

        return vo;
    }

    @Data
    @ApiModel(value = "分类视图")
    public static class SmallCategory {
        @ApiModelProperty("id")
        private Long id;
        @ApiModelProperty("分类标题")
        private String title;
        @ApiModelProperty("描述")
        private String simpleDesc;
    }

    public SmallCategory toSmallCategory() {
        Category.SmallCategory smallCategory = new Category.SmallCategory();
        smallCategory.id = this.id;
        smallCategory.title = this.title;
        smallCategory.simpleDesc = this.simpleDesc;
        return smallCategory;
    }

}